package com.kx.controller;

import com.kx.bean.ParkingSpace;
import com.kx.bean.User;
import com.kx.service.ParkingSpaceService;
import com.kx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ParkingSpaceController {

    @Autowired
    private ParkingSpaceService parkingSpaceService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/findAllSpace", method = RequestMethod.GET)
    @ResponseBody
    public Object findUserData(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        return parkingSpaceService.findData(page, limit);
    }

    @RequestMapping("/getParkingSpaceCount")
    @ResponseBody
    public Integer getAllParkingSpace() {
        return parkingSpaceService.findParkingSpaceCount();
    }

    @RequestMapping("/findReleaseSpace")
    @ResponseBody
    public Integer findReleaseSpace() {
        return parkingSpaceService.findReleaseSpace();
    }

    @RequestMapping("/updateSpaceByIdAndUname")
    @ResponseBody
    public Integer updateSpaceById(ParkingSpace ps, User user) {
        int i = 0;
        if (ps != null && user != null) {
            user.setPid(ps.getParkingNumber());
            userService.updateByPrimaryKeySelective(user);
            i = parkingSpaceService.updateSpaceById(ps);
        } else {
            return i;
        }
        return i;
    }

    @RequestMapping("/updateSpace")
    @ResponseBody
    public Integer updateSpace(ParkingSpace space) {
        int i = 0;
        if (space.getAreaCode() != null && space.getFloorNumber() != null && space.getParkingNumber() != 0) {
            i = parkingSpaceService.updateSpace(space);
        } else {
            return i;
        }
        return i;
    }

    @RequestMapping("/deleteSpace")
    @ResponseBody
    public Integer deleteSpace(Integer pid) {
        return parkingSpaceService.deleteSpace(pid);
    }

    @RequestMapping("/insertSpace")
    @ResponseBody
    public Integer insertSpace(ParkingSpace space) {
        int i = 0;
        if (space.getAreaCode() != null && space.getFloorNumber() != null && space.getParkingNumber() != 0) {
            i = parkingSpaceService.insertSpace(space);
        } else {
            return i;
        }
        return i;
    }

}